# 普通元素分组求数量，数量倒叙排序
nums = [11, 22, 33, 44, 11, 22, 11]

# dic(字典)
group_count = {}
# 统计数字数量
for num in nums:
    if num in group_count:
        group_count[num] += 1
    else:
        group_count[num] = 1
# reversed函数反转一个可迭代对象（如列表、元组、字符串等）的顺序
# 排序： reverse=True: 倒序; reverse=False:正序(sorted 不传reverse参数默认是正序);
group_count_sort = dict(sorted(group_count.items(), key=lambda item: item[1], reverse=True))
print(f'\n{group_count_sort}')  # 结果 {11: 3, 22: 2, 33: 1, 44: 1}


# 实体类列表分组求数量，数量倒叙排序
class Person(object):
    def __init__(self, name, age, gender):
        self.name = name
        self.age = age
        self.gender = gender


list_person = [
    Person('张三', 23, '男'),
    Person('李四', 25, '女'),
    Person('王五', 23, '男'),
    Person('赵六', 25, '男'),
    Person('孙七', 24, '女'),
    Person('周八', 23, '男'),
    Person('吴九', 28, '男'),
    Person('郑十', 20, '女'),
]

# dic(字典)
group_age_count = {}
# 列表取出年龄作为字典的key,统计年龄的数量作为值
for persion in list_person:
    if persion.age in group_age_count:
        group_age_count[persion.age] += 1
    else:
        group_age_count[persion.age] = 1

# 在 Python 中，字典（dict）的items()方法用于返回一个包含字典中所有键值对的可迭代对象
# group_age_count.items()结果为： dict_items([(23, 3), (25, 2), (24, 1), (28, 1), (20, 1)])
# item是元组
# item[1]元组里的年龄数量
group_count_sort = dict(sorted(group_age_count.items(), key=lambda item: item[1], reverse=True))
print(f'\n{group_count_sort}')  # 结果 {23: 3, 25: 2, 24: 1, 28: 1, 20: 1}
